#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'

from pymongo import MongoClient, ASCENDING, DESCENDING
import sys

default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
    reload(sys)
    sys.setdefaultencoding(default_encoding)


# 连接MongoDB
def InitMongoDB():
    MONGODB_HOST = 'localhost'
    MONGODB_PORT = 27017
    MONGODB_DB_NAME = 'ClinicalTrials'
    MONGODB_COLLECTION_NAME = 'ClinicalTrials'
    connection = MongoClient(MONGODB_HOST, MONGODB_PORT)
    db = connection[MONGODB_DB_NAME]
    collection = db[MONGODB_COLLECTION_NAME]
    return connection, collection


# 关闭MongoDB
def CloseMongoDB(MongoDB_Connection):
    MongoDB_Connection.close()


def ParseClinicalStudy(parent_path, MongoDB_Collection):
    print "Start parsing clinical_study.txt"
    count = 0
    currentline = ""
    infile = open(parent_path + "/clinical_study.txt", "r")
    title = infile.readline().rstrip("\r\n")
    split_title = title.split("|")
    for line in infile:
        line = line.rstrip("\r\n")
        if line.startswith("NCT"):
            if currentline != "":
                count += 1
                MongoDB_Collection.insert_one(ParseClinicalStudyLine(currentline[:-1], split_title))
                PrintCount(count, 10000)
                currentline = ""
        currentline += line + "\n"
    if currentline != "":
        count += 1
        MongoDB_Collection.insert_one(ParseClinicalStudyLine(currentline[:-1], split_title))
        PrintCount(count, 10000)
    infile.close()
    MongoDB_Collection.create_index([("NCT_ID", ASCENDING)])
    print "Intotal " + str(count) + " records has been parsed."
    print "Finish parsing clinical_study.txt"


def ParseClinicalStudyLine(line, split_title):
    set_ht = {}
    splitline = line.split("|")
    for i in xrange(45):
        if splitline[i] != "":
            set_ht[split_title[i]] = splitline[i]
    return set_ht


def PrintCount(count, step):
    if count % step == 0:
        print str(count) + " records has been handled."


if __name__ == '__main__':
    MongoDB_Connection, MongoDB_Collection = InitMongoDB()

    ParseClinicalStudy("/local/home/dataadmin/ClinicalTrials", MongoDB_Collection)

    CloseMongoDB(MongoDB_Connection)
